package com.rahms.server.controller;

import com.rahms.common.constant.MessageConstant;
import com.rahms.common.result.R;
import com.rahms.pojo.entity.DrugPlan;
import com.rahms.server.service.DrugPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用药计划控制器
 */
@RestController
@RequestMapping("/drug-plan")
public class DrugPlanController {
    
    @Autowired
    private DrugPlanService drugPlanService;
    
    /**
     * 获取老人的用药计划列表
     */
    @GetMapping("/list")
    public R<List<DrugPlan>> getDrugPlansByElderlyId(@RequestParam Long elderlyId) {
        List<DrugPlan> list = drugPlanService.getDrugPlansByElderlyId(elderlyId);
        return R.success(list);
    }
    
    /**
     * 获取生效中的用药计划
     */
    @GetMapping("/active")
    public R<List<DrugPlan>> getActiveDrugPlans() {
        List<DrugPlan> list = drugPlanService.getActiveDrugPlans();
        return R.success(list);
    }
    
    /**
     * 获取用药计划详情
     */
    @GetMapping("/detail")
    public R<DrugPlan> getDrugPlanDetail(@RequestParam Long planId) {
        DrugPlan drugPlan = drugPlanService.getById(planId);
        return drugPlan != null ? R.success(drugPlan) : R.error(MessageConstant.PLAN_NOT_EXIST);
    }
    
    /**
     * 添加用药计划
     */
    @PostMapping("/add")
    public R<Boolean> addDrugPlan(@RequestBody DrugPlan drugPlan) {
        boolean result = drugPlanService.save(drugPlan);
        return result ? R.success(true).add("message", MessageConstant.PLAN_ADD_SUCCESS) : R.error(MessageConstant.SAVE_FAILED);
    }
    
    /**
     * 更新用药计划
     */
    @PostMapping("/update")
    public R<Boolean> updateDrugPlan(@RequestBody DrugPlan drugPlan) {
        boolean result = drugPlanService.updateById(drugPlan);
        return result ? R.success(true).add("message", MessageConstant.PLAN_UPDATE_SUCCESS) : R.error(MessageConstant.UPDATE_FAILED);
    }
    
    /**
     * 删除用药计划
     */
    @PostMapping("/delete")
    public R<Boolean> deleteDrugPlan(@RequestParam Long planId) {
        boolean result = drugPlanService.removeById(planId);
        return result ? R.success(true).add("message", MessageConstant.PLAN_DELETE_SUCCESS) : R.error(MessageConstant.DELETE_FAILED);
    }
}